//
//  MainTabBarController.m
//  Weibo
//
//  Created by student on 16/4/22.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "MainTabBarController.h"
#import "MessageTableViewController.h"
#import "HomeTableViewController.h"
#import "ProfileViewController.h"
#import "DiscoverTableViewController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.tintColor=[UIColor orangeColor];
    
    
    [self addChildViewControllers];
    }

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self addComposeBtn];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addChildViewControllers

{
    //读取JSON
    NSString *path=[[NSBundle mainBundle] pathForResource:@"MainVCSettings" ofType:@"json"];
    NSData *data=[NSData dataWithContentsOfFile:path];
    
    NSArray *arr=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    for(NSDictionary *dict in arr)
    {
        NSLog(@"%@ %@ %@ ",dict[@"vcName"],dict[@"title"],dict[@"imageName"]);
        
        [self addChildViewController:dict[@"vcName"] imageName:dict[@"imageName"] title:dict[@"title"]];
    }
    
    
//        [self addChildViewController:[[HomeTableViewController alloc]init] imageName:@"tabbar_home" title:@"首页"];
//        [self addChildViewController:[[MessageTableViewController alloc]init] imageName:@"tabbar_message_center" title:@"消息"];
//        [self addChildViewController:[[UIViewController alloc]init] imageName:@"" title:@""];
//        [self addChildViewController:[[DiscoverTableViewController alloc]init] imageName:@"tabbar_discover" title:@"发现"];
//        [self addChildViewController:[[ProfileViewController alloc]init] imageName:@"tabbar_profile" title:@"我"];
}


//-(void)addChildViewController:(UIViewController *)vc imageName:(NSString *)imageName  title:(NSString *)title

-(void)addChildViewController:(NSString *)vcName imageName:(NSString *)imageName  title:(NSString *)title
{
    
    //1.home
    UIViewController *vc=[[NSClassFromString(vcName) alloc]init];
    
    vc.tabBarItem.image=[UIImage imageNamed:imageName];
    vc.tabBarItem.title=title;
    
    vc.tabBarItem.selectedImage=[UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]];
    
    UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:vc];
    
    [self addChildViewController:navi];
    
    
}

-(void)addComposeBtn{
    UIButton *btn=[[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    [btn addTarget:self action:@selector(composeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    int width=[UIScreen mainScreen].bounds.size.width/5;
    btn.frame=CGRectMake(width*2, 0, width, 49);
    
    [self.tabBar addSubview:btn];
    
}

-(void)composeBtnPressed
{
    NSLog(@"127886");
}
@end
